package com.bestcem.xm.member.grpc.service;

import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.EnumUtil;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import com.bestcem.xm.member.enums.MemberTypeEnum;
import com.bestcem.xm.member.grpc.service.param.PageParam;
import com.bestcem.xm.member.grpc.service.param.member.ListOrgMembersParams;
import com.bestcem.xm.member.grpc.v1.common.Member;
import com.bestcem.xm.member.grpc.v1.services.ListMembersByOrgIdRequest;
import com.bestcem.xm.member.grpc.v1.services.ListMembersByOrgIdResponse;
import com.bestcem.xm.member.grpc.v1.services.TicketServiceGrpc;
import com.bestcem.xm.member.service.TicketMemberService;
import com.bestcem.xm.member.service.dto.MemberDTO;
import com.bestcem.xm.member.service.dto.PageDTO;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author dc.wang <dc.wang@idiaoyan.com>
 * @version V1.0
 * @description 工单依赖grpc接口
 * @date 2021/9/10
 */
@GrpcService
public class TicketMemberGrpcServerService extends TicketServiceGrpc.TicketServiceImplBase {
    private final TicketMemberService ticketMemberService;

    @Autowired
    public TicketMemberGrpcServerService(TicketMemberService ticketMemberService){
        this.ticketMemberService = ticketMemberService;
    }

    @Override
    public void listMembersByOrgId(ListMembersByOrgIdRequest request, StreamObserver<ListMembersByOrgIdResponse> responseObserver) {
        ListMembersByOrgIdResponse.Builder builder = ListMembersByOrgIdResponse.newBuilder();

        // 组装参数
        ListOrgMembersParams params = new ListOrgMembersParams();
        params.setOrgId(request.getOrgId());
        MemberTypeEnum memberTypeEnum = EnumUtil.getEnumByIndex(MemberTypeEnum.class, request.getMemberTypeValue());
        params.setMemberTypeEnum(memberTypeEnum);
        params.setPageParam(new PageParam(request.getPage().getPage(), request.getPage().getSize()));

        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(params);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        // 分页获取联系人信息
        ServiceResult<PageDTO<MemberDTO>> result = ticketMemberService.listOrgMembers(params);

        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
            builder.setTotal(result.getData().getTotal());
            List<Member> members = result.getData().getRows().stream().map(this::convert).collect(Collectors.toList());
            builder.addAllMembers(members);
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    private Member convert(MemberDTO memberDTO){
        Member.Builder res = Member.newBuilder();

        res.setId(memberDTO.getId());
        if(StringUtils.isNotBlank(memberDTO.getName())){
            res.setName(memberDTO.getName());
        }
        if(StringUtils.isNotBlank(memberDTO.getMobile())){
            res.setPhone(memberDTO.getMobile());
        }
        if(StringUtils.isNotBlank(memberDTO.getEmail())){
            res.setEmail(memberDTO.getEmail());
        }
        return res.build();
    }
}
